unit uWaptMain;

interface
uses Classes, SysUtils, uWaptMisc;
Const
            FILEEXTENSION_MANUAL    =     '.man';

            // The size AND Order of this array must be maintained, new additions to be
            // appended only to it. Otherwise GetParamType will be out of whack
            SUPPORTED_PARAMS        :     array[0..13] of string = ('-update','-upgrade',
                                          '-install','-remove','-source','-check',
                                          '-clean','-autoclean','-search','-download',
                                          '-cache', '-listinstalled', '-createprofile',
                                          '-test');

            // a match representing the element in the array should be added here too.
            // The handler is in the ProcessCommandLine function

Type        TSupportedParams        =     (spUpdate, spUpgrade, spInstall, spRemove, spSource,
                                          spCheck, spClean, spAutoClean, spSearch, spDownload,
                                          spCache, spListInstalled, spcreateprofile, spTest);


          PROCEDURE Init();
          FUNCTION IsParamSupported(aParam : string) : boolean;
          FUNCTION ProcessCommandLine() : boolean;
          PROCEDURE ShowHelp();
          FUNCTION GetParamType(aParam : String) : TSupportedParams;

implementation

FUNCTION GetParamType(aParam : String) : TSupportedParams;
Begin
// The order or the items in the array must be maintained
Result := spAutoClean;
if aParam = SUPPORTED_PARAMS[0] then Result := spUpdate;
if aParam = SUPPORTED_PARAMS[1] then Result := spUpgrade;
if aParam = SUPPORTED_PARAMS[2] then Result := spInstall;
if aParam = SUPPORTED_PARAMS[3] then Result := spRemove;
if aParam = SUPPORTED_PARAMS[4] then Result := spSource;
if aParam = SUPPORTED_PARAMS[5] then Result := spCheck;
if aParam = SUPPORTED_PARAMS[6] then Result := spClean;
if aParam = SUPPORTED_PARAMS[7] then Result := spAutoClean;
if aParam = SUPPORTED_PARAMS[8] then Result := spSearch;
if aParam = SUPPORTED_PARAMS[9] then Result := spDownload;
if aParam = SUPPORTED_PARAMS[10] then Result := spCache;
if aParam = SUPPORTED_PARAMS[11] then Result := spListInstalled;
if aParam = SUPPORTED_PARAMS[12] then Result := spCreateProfile;
if aParam = SUPPORTED_PARAMS[13] then Result := spTest;

End;

FUNCTION IsParamSupported(aParam : String) : boolean;
Var
  i   :   integer;
Begin
for i := Low(SUPPORTED_PARAMS) to High(SUPPORTED_PARAMS) do
     Begin
       if aParam = SUPPORTED_PARAMS[i] then
          Begin
            Result := True;
            Exit;
          End;
     End;
Result := False;
End;

// =============================================================================
PROCEDURE Init();
Var
  Temp : char;
Begin
if ProcessCommandLine then
   Begin

     WriteLn ('');
     WriteLn ('Processed CommandLine OK');
//     ReadLn(Temp);
   End
else
   Begin
     WriteLn ('Type ' + ExtractFileName(ParamStr(0)) + ' -h for help');
   End;
End;
// =============================================================================



// =============================================================================
FUNCTION ProcessCommandLine() : boolean;
Var
    i       :     integer;
    v       :     integer;
    x       :     integer;
    TempList:     TStringList;
    OptionsOk : boolean;
Begin
OptionsOk := false;
if ParamCount > 0 then
   Begin
   TempList := TStringList.Create;

   // do something
   Result := True;
   for i := 1 to ParamCount do
        Begin
        if ParamStr(i) = '-h' then
            Begin
            WriteLn(ParamStr(i));
            ShowHelp;
            Result := False;
            Exit;
            End;

        if IsParamSupported(ParamStr(i)) = true then
           Begin
             // do some further processing for this paramater
             case GetParamType(ParamStr(i)) of
                  spTest        :     Begin
                                      // Just output something
                                      WriteLn(IntToStr(ParamCount));
                                      for x := 1 to ParamCount do
                                         Begin
                                         WriteLn(ParamStr(x));
                                         End;
                                      End;
                  spUpdate      :     Begin

                                      End;
                  spUpgrade     :     Begin

                                      End;
                  spInstall     :     Begin

                                      End;
                  spSource      :     Begin

                                      End;
                  spCheck       :     Begin

                                      End;
                  spClean       :     Begin

                                      End;
                  spAutoClean   :     Begin

                                      End;
                  spSearch      :     Begin

                                      End;
                  spDownload    :     Begin

                                      End;
                  spCache       :     Begin

                                      End;
                  spListInstalled:    Begin
                                      // List Windows Installed Programs capable of Uninstall
                                      TempList.Clear;
                                      uWaptMisc.ListInstalledApps(TempList, false);

                                      WriteLn('List of Windows Installed Software');
                                      WriteLn('==================================');

                                      for v := 0 to TempList.Count - 1 do
                                           Begin
                                           WriteLn(TempList.Strings[v]);
                                           End;
                                      WriteLn('Count: ' + IntToStr(TempList.Count - 1));
                                      WriteLn('==================================');
                                      End;
                  spRemove :          Begin
                                      uWaptMisc.UninstallApp(ParamStr(i + 1));
                                      //                                      WriteLn(ParamStr(i + 1));
                                      //uwaptmisc.UninstallApp();
                                      OptionsOk := true;
                                      End;

             end;
           End
        else
           Begin
           if not OptionsOk then
              Begin
              WriteLn('Unsupported Parameter "' + ParamStr(i) + '" passed to ' + ExtractFileName(ParamStr(0)));
              Result := False;
              End;
           End;
        End;
    TempList.Free;
   End
ELSE
   Begin
     Result := True;
     // Return true here, because there are no paramaters to parse but nothing
     // has gone wrong - should do something usefull here
     ShowHelp();
   End;
End;
// =============================================================================




// =============================================================================
PROCEDURE ShowHelp();
Var
  aManualFile : TextFile;
  aText       : string;
  i           : integer;
Begin
  // if a Manual exists under the exe's filename + manual extension then
  // show that file instead, else, show basic help
  if FileExists(ExtractFileName(ParamStr(0)) + FILEEXTENSION_MANUAL)then
     Begin
      try
          Begin
          AssignFile(aManualFile, ParamStr(0) + FILEEXTENSION_MANUAL);
          Reset(aManualFile);
          while not Eof(aManualFile) do
           begin
            ReadLn(aManualFile, aText);
            WriteLn(aText);
           End;
           CloseFile(aManualFile);
          End;
      Except
          Begin
          WriteLn('');
          WriteLn('Problem while trying to open file ' + ParamStr(0) + FILEEXTENSION_MANUAL);
          WriteLn('');
          End;
      end;
     End
  ELSE
      Begin
        // Show basic help
        WriteLn('Basic Commandline Usage for ' + ExtractFileName(ParamStr(0)));
        WriteLn('');
        WriteLn('   Type: ');
        WriteLn('       ' + ExtractFileName(ParamStr(0)) + ' -h          :     basic help');
        WriteLn('       ' + ExtractFileName(ParamStr(0)) + ' <param>=<data>');
        WriteLn('');
        WriteLn('There should have existed a simple manual file for ' + ExtractFileName(ParamStr(0) + ' but it was not found'));
        WriteLn('As a result, what you see above is only basic usage instructions for passing paramaters to this program and not an actual manual');
        WriteLn('');
        WriteLn('Internal List of Supported Commands: ');
        WriteLn('');
        for i := Low(SUPPORTED_PARAMS) to High(SUPPORTED_PARAMS) do
             Begin
             WriteLn('      ' + SUPPORTED_PARAMS[i]);
             End;
      End;
End;
// =============================================================================
end.
